<?php
/**
 * 邮件操作类
 * 
 * Created		: 2011-12-10
 * Modified		: 2011-12-10
 * @version	: 0.1.0
 * @author		: Joseph Chen (jsph.chen@gmail.com)
 */
class Mailer
{
	public static $mailer = null;
	
	public static $debug = false;
	
	/**
	 * 初始化参数
	 */
	public static function init() 
	{
		$cfgFile = APP_ROOT.'Config'.DS.'email.cfg.php';
		include($cfgFile);
		
		$mail = self::getMailer();
		
		$mail->IsSMTP();
		$mail->IsHTML();
		$mail->CharSet		= 'UTF-8';
		$mail->SMTPDebug	= self::$debug;
		
		if ($smtp_ssl) {
			$mail->SMTPSecure	= "ssl";
		}
		$mail->SMTPAuth		= true;
		$mail->Port			= $smtp_port;
		$mail->Host			= $smtp_host;
		$mail->Username		= $smtp_user;
		$mail->Password		= $smtp_pass;
		$mail->SetFrom($smtp_email, $smtp_fromname);
	}
	
	public static function setDebug($debug=true) 
	{
		self::$debug = $debug;
	}
	
	/**
	 * 获取类实例
	 */
	public static function getMailer() 
	{
		if (!self::$mailer) {
			self::$mailer = new PHPMailer();
		}
		return self::$mailer;
	}
	
	/**
	 * 发送邮件
	 * @param string $subject
	 * @param string $body
	 * @param string $to_email
	 * @param string $to_name
	 */
	public static function send($subject, $body, $to_email, $to_name='')
	{
		if (!self::$mailer) {
			self::init();
		}
		
		$mail = self::getMailer();
		
		$mail->AddAddress($to_email, $to_name);
		$mail->Subject = $subject;
		$mail->MsgHTML($body);
		$ret = $mail->Send();
		$mail->SmtpClose();
		return $ret;
	}
}
